﻿using Commons.ASPNETCore;
using FileService.Domain;
using FileService.Domain.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FileService.WebApi.Controllers
{
    [Route("BaseVideo/[controller]")]
    [ApiController]
    public class VideoController : ControllerBase
    {
        private readonly ITabRepository repository;
        public VideoController(ITabRepository repository)
        {
            this.repository = repository;
        }

        [HttpGet]
        public async Task<Result> FindListByTabId(long tabId)
        {
            var list = await repository.QueryListByParentIdAsync<Videos>(tabId);
            return Result.Success(list);
        }
        [HttpGet("{id}")]
        public async Task<Result> Get(long id)
        {
            var a = await repository.FindByIdAsync<Videos>(id);
            return Result.Success(a);
        }

        [HttpPost]
        public async Task<Result> Add(VideoRequest request)
        {
            var result = await repository.AddAsync(Videos.Create(request.Name, request.TabId));
            if (result == 0)
            {
                return Result.Fail();
            }
            return Result.Success();
        }

        [HttpDelete]
        public async Task<Result> Delete([FromBody] Videos request)
        {
            var re = await repository.DeleteAsync(request);
            return Result.Success(re);
        }

        [HttpPut]
        public async Task<Result> Put([FromBody] Videos request)
        {
            var o = await repository.FindByIdAsync<Videos>(request.Id);
            if (o == null)
            {
                return Result.Fail();
            }

            var re = await repository.UpdateAsync(o.SetFile(request.FileUrl, request.FileName,request.Img));
            return Result.Success(re);
        }
    }
}
